home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gdevescp.c < prev    next >
C/C++ Source or Header  |  1993-05-26  |  11KB  |  408 lines

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevescp2.c */
  20. /*
  21.  * Epson 'ESC/P 2' language printer driver for Ghostscript.
  22.  *
  23.  * This driver uses the ESC/P2 language raster graphics commands with
  24.  * compression. The driver skips vertical white space, provided that
  25.  * the white space is >= 24/band_size (<~ 1.7mm @ 360dpi!) high. There
  26.  * is no attempt to skip horizontal white space, but the compression
  27.  * greatly reduces the significance of this (a nearly blank line would
  28.  * take about 45 bytes). The driver compresses the data one scan line at
  29.  * a time, even though this is not enforced by the hardware. The reason
  30.  * I have done this is that, since the driver skips data outside the
  31.  * margins, we would have to set up a extra pointers to keep track of
  32.  * the data from the previous scan line. Doing this would add extra
  33.  * complexity at a small saving of disk space.
  34.  *
  35.  * These are the only possible optimisations that remain, and would
  36.  * greatly increase the complexity of the driver. At this point, I don't
  37.  * consider them necessary, but I might consider implementing them if
  38.  * enough people encourage me to do so.
  39.  *
  40.  * Richard Brown (rab@tauon.ph.unimelb.edu.au)
  41.  *
  42.  */
  43. #include "gdevprn.h"
  44.  
  45. /*
  46.  * Valid values for X_DPI and Y_DPI: 180, 360
  47.  *
  48.  * The value specified at compile time is the default value used if the
  49.  * user does not specify a resolution at runtime.
  50.  */
  51. #ifndef X_DPI
  52. #  define X_DPI 360
  53. #endif
  54.  
  55. #ifndef Y_DPI
  56. #  define Y_DPI 360
  57. #endif
  58.  
  59. /*
  60.  * Margin definitions: Stylus 800 printer driver:
  61.  *
  62.  * The commented margins are from the User's Manual.
  63.  *
  64.  * The values actually used here are more accurate for my printer.
  65.  * The Stylus paper handling is quite sensitive to these settings.
  66.  * If you find that the printer uses an extra page after every real
  67.  * page, you'll need to increase the top and/or bottom margin.
  68.  */
  69.  
  70. #define STYLUS_L_MARGIN 0.13    /*0.12*/
  71. #define STYLUS_B_MARGIN 0.56    /*0.51*/
  72. #define STYLUS_T_MARGIN 0.34    /*0.12*/
  73. #ifdef A4
  74. #   define STYLUS_R_MARGIN 0.18 /*0.15*/
  75. #else
  76. #   define STYLUS_R_MARGIN 0.38
  77. #endif
  78.  
  79. /*
  80.  * The defines below allow future expansion for printers that may
  81.  * require different margin settings from the Stylus 800.
  82.  */
  83.  
  84. #define L_MARGIN STYLUS_L_MARGIN
  85. #define B_MARGIN STYLUS_B_MARGIN
  86. #define R_MARGIN STYLUS_R_MARGIN
  87. #define T_MARGIN STYLUS_T_MARGIN
  88.  
  89. /* The device descriptor */
  90. private dev_proc_print_page(escp2_print_page);
  91.  
  92. /* Epson ESC/P2 device */
  93. gx_device_printer far_data gs_escp2_device =
  94.   prn_device(prn_std_procs, "escp2",
  95.     DEFAULT_WIDTH_10THS,
  96.     DEFAULT_HEIGHT_10THS,            
  97.     X_DPI, Y_DPI,
  98.     L_MARGIN, B_MARGIN, R_MARGIN, T_MARGIN,
  99.     1, escp2_print_page);
  100.  
  101. /* ------ Internal routines ------ */
  102.  
  103. /* Send the page to the printer. */
  104. private int
  105. escp2_print_page(gx_device_printer *pdev, FILE *prn_stream)
  106. {    
  107.  
  108.     int line_size = gdev_prn_raster((gx_device_printer *)pdev);
  109.     int band_size = 24;    /* 1, 8, or 24 */
  110.     int in_size = line_size * band_size;
  111.  
  112.     byte *buf1 = (byte *)gs_malloc(in_size, 1, "escp2_print_page(buf1)");
  113.     byte *buf2 = (byte *)gs_malloc(in_size, 1, "escp2_print_page(buf2)");
  114.     byte *in = buf1;
  115.     byte *out = buf2;
  116.  
  117.     int skip, lnum, top, bottom, left, width;
  118.     int auto_feed = 1;
  119.     int count, i;
  120.  
  121.     /*
  122.     ** Check for valid resolution:
  123.     **
  124.     **    XDPI    YDPI
  125.     **    360    360
  126.     **    360    180
  127.     **    180    180
  128.     */
  129.  
  130.     if( !( (pdev->x_pixels_per_inch == 180 &&
  131.             pdev->y_pixels_per_inch == 180) ||
  132.            (pdev->x_pixels_per_inch == 360 &&
  133.            (pdev->y_pixels_per_inch == 360 ||
  134.             pdev->y_pixels_per_inch == 180) )) )
  135.            return_error(gs_error_rangecheck);
  136.  
  137.     /*
  138.     ** Check buffer allocations:
  139.     */
  140.  
  141.     if ( buf1 == 0 || buf2 == 0 )
  142.     {    if ( buf1 ) 
  143.           gs_free((char *)buf1, in_size, 1, "escp2_print_page(buf1)");
  144.         if ( buf2 ) 
  145.           gs_free((char *)buf2, in_size, 1, "escp2_print_page(buf2)");
  146.         return_error(gs_error_VMerror);
  147.     }
  148.  
  149.     /*
  150.     ** Reset printer, enter graphics mode:
  151.     */
  152.  
  153.     fwrite("\033@\033(G\001\000\001", 1, 8, prn_stream);
  154.  
  155. #ifdef A4
  156.     /*
  157.     ** After reset, the Stylus is set up for US letter paper.
  158.     ** We need to set the page size appropriately for A4 paper.
  159.     ** For some bizarre reason the ESC/P2 language wants the bottom
  160.     ** margin measured from the *top* of the page:
  161.     */
  162.  
  163.     fwrite("\033(U\001\0\n\033(C\002\0t\020\033(c\004\0\0\0t\020",
  164.                                                     1, 22, prn_stream);
  165. #endif
  166.  
  167.     /*
  168.     ** Set the line spacing to match the band height:
  169.     */
  170.  
  171.     if( pdev->y_pixels_per_inch == 360 )
  172.        fwrite("\033(U\001\0\012\033+\030", 1, 9, prn_stream);
  173.     else
  174.        fwrite("\033(U\001\0\024\033+\060", 1, 9, prn_stream);
  175.  
  176.         /*
  177.         ** If the printer has automatic page feeding, then the paper
  178.         ** will already be positioned at the top margin value, so we
  179.         ** start printing the image from there. Similarly, we must not
  180.         ** try to print or even line feed past the bottom margin, since
  181.         ** the printer will automatically load a new page.
  182.         ** Printers without this feature may actually need to be told
  183.         ** to skip past the top margin.
  184.         */
  185.  
  186.         if( auto_feed ) {
  187.            top = T_MARGIN * pdev->y_pixels_per_inch;
  188.            bottom = pdev->height - B_MARGIN * pdev->y_pixels_per_inch;
  189.         } else {
  190.            top = 0;
  191.            bottom = pdev->height;
  192.         }
  193.  
  194.         /*
  195.         ** Make left margin and width sit on byte boundaries:
  196.         */
  197.  
  198.         left  = ( (int) (L_MARGIN * pdev->x_pixels_per_inch) ) >> 3 ;
  199.  
  200.         width = ((pdev->width - (int)(R_MARGIN * pdev->x_pixels_per_inch)) >> 3) - left;
  201.  
  202.     /*
  203.     ** Print the page:
  204.     */
  205.  
  206.     for ( lnum = top, skip = 0 ; lnum < bottom ; )
  207.     {    
  208.         byte *in_data;
  209.         byte *inp;
  210.         byte *in_end;
  211.         byte *outp;
  212.         register byte *p, *q;
  213.         int lcnt;
  214.  
  215.         /*
  216.         ** Check buffer for 0 data. We can't do this mid-band
  217.         */
  218.  
  219.         gdev_prn_get_bits(pdev, lnum, in, &in_data);
  220.         while ( in_data[0] == 0 &&
  221.                 !memcmp((char *)in_data, (char *)in_data + 1, line_size - 1) &&
  222.                 lnum < bottom )
  223.             {    
  224.             lnum++;
  225.             skip++;
  226.             gdev_prn_get_bits(pdev, lnum, in, &in_data);
  227.         }
  228.  
  229.         if(lnum == bottom ) break;    /* finished with this page */
  230.  
  231.         /*
  232.         ** Skip blank lines if we need to:
  233.         */
  234.  
  235.         if( skip ) {
  236.            fwrite("\033(v\002\000", 1, 5, prn_stream);
  237.            fputc(skip & 0xff, prn_stream);
  238.            fputc(skip >> 8,   prn_stream);
  239.            skip = 0;
  240.         }
  241.  
  242.         lcnt = gdev_prn_copy_scan_lines(pdev, lnum, in, in_size);
  243.  
  244.         /*
  245.         ** Check to see if we don't have enough data to fill an entire
  246.         ** band. Padding here seems to work (the printer doesn't jump
  247.         ** to the next (blank) page), although the ideal behaviour
  248.         ** would probably be to reduce the band height.
  249.         **
  250.         ** Pad with nulls:
  251.         */
  252.  
  253.         if( lcnt < band_size )
  254.            memset(in + lcnt * line_size, 0, in_size - lcnt * line_size);
  255.  
  256.         /*
  257.         ** Now we have a band of data: try to compress it:
  258.         */
  259.  
  260.         for( outp = out, i = 0 ; i < band_size ; i++ ) {
  261.  
  262.            /*
  263.            ** Take margins into account:
  264.            */
  265.  
  266.            inp = in + i * line_size + left;
  267.            in_end = inp + width;
  268.  
  269.            /*
  270.            ** walk through input buffer, looking for repeated data:
  271.            ** Since we need more than 2 repeats to make the compression
  272.            ** worth it, we can compare pairs, since it doesn't matter if we
  273.            **
  274.            */
  275.  
  276.            for( p = inp, q = inp + 1 ; q < in_end ; ) {
  277.  
  278.                if( *p != *q ) {
  279.  
  280.                   p += 2;
  281.                   q += 2;
  282.  
  283.                } else {
  284.  
  285.                   /*
  286.                   ** Check behind us, just in case:
  287.                   */
  288.  
  289.                   if( p > inp && *p == *(p-1) )
  290.                      p--;
  291.  
  292.                /*
  293.                ** walk forward, looking for matches:
  294.                */
  295.  
  296.                for( q++ ; *q == *p && q < in_end ; q++ ) {
  297.                   if( (q-p) >= 128 ) {
  298.                      if( p > inp ) {
  299.                         count = p - inp;
  300.                         while( count > 128 ) {
  301.                        *outp++ = '\177';
  302.                        memcpy(outp, inp, 128);    /* data */
  303.                        inp += 128;
  304.                        outp += 128;
  305.                        count -= 128;
  306.                         }
  307.                         *outp++ = (char) (count - 1); /* count */
  308.                         memcpy(outp, inp, count);    /* data */
  309.                         outp += count;
  310.                      }
  311.                  *outp++ = '\201';    /* Repeat 128 times */
  312.                  *outp++ = *p;
  313.                      p += 128;
  314.                      inp = p;
  315.                   }
  316.                }
  317.  
  318.                if( (q - p) > 2 ) {    /* output this sequence */
  319.                   if( p > inp ) {
  320.                  count = p - inp;
  321.                  while( count > 128 ) {
  322.                     *outp++ = '\177';
  323.                     memcpy(outp, inp, 128);    /* data */
  324.                     inp += 128;
  325.                     outp += 128;
  326.                     count -= 128;
  327.                  }
  328.                  *outp++ = (char) (count - 1);    /* byte count */
  329.                  memcpy(outp, inp, count);    /* data */
  330.                  outp += count;
  331.                   }
  332.                   count = q - p;
  333.                   *outp++ = (char) (256 - count + 1);
  334.                   *outp++ = *p;
  335.                   p += count;
  336.                   inp = p;
  337.                } else    /* add to non-repeating data list */
  338.                   p = q;
  339.                if( q < in_end )
  340.                   q++;
  341.                }
  342.            }
  343.  
  344.            /*
  345.            ** copy remaining part of line:
  346.            */
  347.  
  348.            if( inp < in_end ) {
  349.  
  350.               count = in_end - inp;
  351.  
  352.               /*
  353.               ** If we've had a long run of varying data followed by a
  354.               ** sequence of repeated data and then hit the end of line,
  355.               ** it's possible to get data counts > 128.
  356.               */
  357.  
  358.               while( count > 128 ) {
  359.             *outp++ = '\177';
  360.             memcpy(outp, inp, 128);    /* data */
  361.             inp += 128;
  362.             outp += 128;
  363.             count -= 128;
  364.               }
  365.  
  366.               *outp++ = (char) (count - 1);    /* byte count */
  367.               memcpy(outp, inp, count);    /* data */
  368.               outp += count;
  369.            }
  370.         }
  371.  
  372.         /*
  373.         ** Output data:
  374.         */
  375.  
  376.             fwrite("\033.\001", 1, 3, prn_stream);
  377.  
  378.             if(pdev->y_pixels_per_inch == 360)
  379.                fputc('\012', prn_stream);
  380.         else
  381.                fputc('\024', prn_stream);
  382.  
  383.             if(pdev->x_pixels_per_inch == 360)
  384.                fputc('\012', prn_stream);
  385.         else
  386.                fputc('\024', prn_stream);
  387.  
  388.         fputc(band_size, prn_stream);
  389.  
  390.             fputc((width << 3) & 0xff, prn_stream);
  391.         fputc( width >> 5,         prn_stream);
  392.  
  393.             fwrite(out, 1, (outp - out), prn_stream);
  394.  
  395.             fwrite("\r\n", 1, 2, prn_stream);
  396.         lnum += band_size;
  397.     }
  398.  
  399.     /* Eject the page and reinitialize the printer */
  400.  
  401.     fputs("\f\033@", prn_stream);
  402.     fflush(prn_stream);
  403.  
  404.     gs_free((char *)buf2, in_size, 1, "escp2_print_page(buf2)");
  405.     gs_free((char *)buf1, in_size, 1, "escp2_print_page(buf1)");
  406.     return 0;
  407. }
  408.